<?php
require_once 'cofd/functions.php';
$directory = 'novel/'.encrypt($_GET['novel'],'D','xhpayencrypt'); 
$currentChapterFile = encrypt($_GET['zj'],'D','xhpayencrypt').'.txt'; 
if (file_exists("{$directory}/{$currentChapterFile}")) {
    $chapterContent = file_get_contents("{$directory}/{$currentChapterFile}");
    $chapterName = pathinfo($currentChapterFile, PATHINFO_FILENAME); 
    $allChapterFiles = scandir($directory);
    $allChapterFiles = array_filter($allChapterFiles, function($file) use ($directory) {
        return is_file($directory . DIRECTORY_SEPARATOR . $file);
    });
    $currentIndex = array_search($currentChapterFile, $allChapterFiles);
    $previousChapter = $allChapterFiles[$currentIndex - 1] ?? null;
    $nextChapter =  $allChapterFiles[$currentIndex+1] ?? null;
} else {
    die('此章节不存在');
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($chapterName); ?></title>
    <link rel="stylesheet" href="ast/xsnr.css">
</head>
<body>
    <div class="stars"></div> 
    <div class="container">
        <div class="chapter-content">
            <h1><?php echo htmlspecialchars($chapterName); ?></h1>
            <p style="font-size: 1.3em;"><?php echo nl2br(htmlspecialchars($chapterContent)); ?></p>
        </div>
        <div class="navigation-buttons">
            <?php if ($previousChapter): ?>
                <a href="?novel=<?php echo urlencode($_GET['novel']); ?>&zj=<?php echo urlencode(encrypt((explode('.', $previousChapter))[0],'E','xhpayencrypt')); ?>">上一章</a>
            <?php endif; ?>
            <?php if ($nextChapter): ?>
                <a href="?novel=<?php echo urlencode($_GET['novel']); ?>&zj=<?php echo urlencode(encrypt((explode('.', $nextChapter))[0],'E','xhpayencrypt')); ?>">下一章</a>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
<script>
</script>